<?php
/* QQ:34734852
 * 业务联系：小鱼
 */
class Upload{
	
	private $File    = array();
	public  $type    = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif');
	public  $ext     = array('jpeg', 'jpg', 'png', 'gif');
	public  $size    = 512;
	public  $small   = array('w' => 0, 'h' => 0);
	public  $newname = true;
	private $Rult    = array('rult' => false, 'msg' => '', 'code' => 0, 'data' => array('big' => '', 'small' => ''));
	private $xq      = array();
	
	
	public function request( $key = 'file' ) {
		$this -> File = $_FILES[ $key ];
		$this -> Bigin();
		return $this -> Rult;
	}
	
	private function Bigin() {
		if ( $this -> File['error'] ) { //检测上传错误
		   $this -> error();
		} elseif (!in_array($this -> File['type'], $this -> type)) { //文件类型检测
		   $this -> Rult['code'] = 5;
		   $this -> Rult['msg']  = '上传的文件类型不允许';
		} else {
		   $this -> xq = pathinfo($this -> File['name']);
		   if ( !in_array($this -> xq['extension'], $this -> ext) ) { //文件扩展名检测
			 $this -> Rult['code'] = 8;
			 $this -> Rult['msg']  = '上传的文件扩展名不允许';
		   } elseif ( ceil( $this -> File['size'] / 1024 ) > $this -> size ) { //文件大小检测
			 $this -> Rult['code'] = 9;
			 $this -> Rult['msg']  = '上传的文件大小超出' . $this -> size . 'KB';
		   } else {
			 $this -> Mydata();
		   }
		}
	}
	
	private function Mydata() {
		$Path    = $this -> path();
		$ImgCode = file_get_contents(  $this -> File['tmp_name'] );
		$this -> Rult['data']['base']  = "data:{$this->File[type]};base64," . base64_encode( $ImgCode );
		if ( move_uploaded_file( $this -> File['tmp_name'], $Path['big']['in'] ) ) {
			 $this -> Rult['rult'] = true;
			 $this -> Rult['code'] = 0;
			 $this -> Rult['msg']  = '';
			 $this -> Rult['data']['big'] = $Path['big']['out'];
			 if ( $this -> small['w'] > 0 && $this -> small['h'] > 0  ) {
				 $this -> ImagesSmall( $Path );
			 }
		} else {
			 $this -> Rult['code'] = 10;
			 $this -> Rult['msg']  = '文件上传失败';
		}
		unset( $ImgCode, $Path );
	}
	
	private function path() {
		$p = array('big' => array('in' => ROOT . APP . '/Upload', 'out' => '/' . APP . '/Upload'), 'small' => array('in' => ROOT . APP . '/Upload', 'out' => '/' . APP . '/Upload'));
		if ( in_array($this -> File['type'], $this -> type ) ) {
			$p['big']['in']    .= '/Images';
			$p['big']['out']   .= '/Images';
			$p['small']['in']  .= '/Images';
			$p['small']['out'] .= '/Images';
		} elseif ( in_array( $this -> File['type'], array('application/x-zip-compressed', 'application/octet-stream') ) ) {
			$p['big']['in']    .= '/ZipRar';
			$p['big']['out']   .= '/ZipRar';
			$p['small']['in']  .= '/ZipRar';
			$p['small']['out'] .= '/ZipRar';
		} else {
			$p['big']['in']    .= '/File';
			$p['big']['out']   .= '/File';
			$p['small']['in']  .= '/File';
			$p['small']['out'] .= '/File';
		}
		$p['big']['in'] .= '/' . date('Y');
		$p['big']['out'] .= '/' . date('Y');
		is_dir($p['big']['in'] ) or mkdir( $p['big']['in'] );
		
		$p['big']['in'] .= '/' . date('m');
		$p['big']['out'] .= '/' . date('m');
		is_dir($p['big']['in'] ) or mkdir( $p['big']['in'] );
		
		$p['big']['in'] .= '/' . date('d');
		$p['big']['out'] .= '/' . date('d');
		is_dir($p['big']['in'] ) or mkdir( $p['big']['in'] );
		
		$Name = date('H-i-s'). '_' . rand();
		if ( $this -> newname ) { 
		  $p['big']['in'] .= '/' . $Name . '.' . $this -> xq['extension'];
		  $p['big']['out'] .= '/' . $Name . '.' .  $this -> xq['extension'];
		} else {
		  $p['big']['in'] .= '/' . $this -> File['name'];
		  $p['big']['out'] .= '/' . $this -> File['name'];
		}
		
		if ( $this -> small['w'] > 0 && $this -> small['h'] > 0 ) {
			  $p['small']['in'] .= '/' . date('Y');
			  $p['small']['out'] .= '/' . date('Y');
			  is_dir($p['small']['in'] ) or mkdir( $p['small']['in'] );
			  
			  $p['small']['in'] .= '/' . date('m');
			  $p['small']['out'] .= '/' . date('m');
			  is_dir($p['small']['in'] ) or mkdir( $p['small']['in'] );
			  
			  $p['small']['in'] .= '/' . date('d');
			  $p['small']['out'] .= '/' . date('d');
			  is_dir($p['small']['in'] ) or mkdir( $p['small']['in'] );
			  
			  if ( $this -> newname ) { 
				$p['small']['in'] .= '/samll_' . $Name . '.' . $this -> xq['extension'];
				$p['small']['out'] .= '/samll_' . $Name . '.' .  $this -> xq['extension'];
			  } else {
				$p['small']['in'] .= '/small_' . $this -> File['name'];
				$p['small']['out'] .= '/small_' . $this -> File['name'];
			  }
			
		} else {
		   unset( $p['small'] );	
		}
		
		return $p;
	}
	
	
	private function ImagesSmall( $path = array() ) {
		list($s_w,$s_h,$exten) = getimagesize($path['big']['in']);
		 switch ( $exten ) {
			 case 1: 
			    $im = imagecreatefromgif( $path['big']['in'] );
				break;
			 case 2:
				$im = imagecreatefromjpeg( $path['big']['in'] );
				break;
			 case 3:
				$im = imagecreatefrompng( $path['big']['in'] );
				break;
			 default:
				return false;
		 }
		 imagesavealpha($im,true);
		 $NewIm =  imagecreatetruecolor($this -> small['w'], $this -> small['h']);
		 imagealphablending($NewIm,false);
		 imagesavealpha($NewIm,true);
		 imagecopyresampled($NewIm, $im, 0, 0, 0, 0, $this -> small['w'], $this -> small['h'], $s_w, $s_h);
		 switch( $exten ) {
		    case 1: imagegif( $NewIm, $path['small']['in']);break;
			case 2: imagejpeg( $NewIm, $path['small']['in']);break;
			case 3: imagepng( $NewIm, $path['small']['in']);break;
			default: return false;
		 }
		 imagedestroy($im);
		 imagedestroy($NewIm);
		 unset( $ChacTemp, $im, $InPath, $Name, $NewIm, $Show );
	     $this -> Rult['data']['small'] = $path['small']['out'];
	}
	
    private function error() {
        switch ($this -> File['error']) {
            case 1:
                $this->Rult['msg'] = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值！';
                break;
            case 2:
                $this->Rult['msg'] = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值！';
                break;
            case 3:
                $this->Rult['msg'] = '文件只有部分被上传！';
                break;
            case 4:
                $this->Rult['msg'] = '没有文件被上传！';
                break;
            case 6:
                $this->Rult['msg'] = '找不到临时文件夹！';
                break;
            case 7:
                $this->Rult['msg'] = '文件写入失败！';
                break;
            default:
                $this->Rult['msg'] = '未知上传错误！';
        }
		$this->Rult['code'] = $this -> File['error'];
    }
	
	
	
	
}